package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.Config;
import cn.livingCloud.context.VelocityFactory;
import cn.livingCloud.dto.MemberListDto;
import cn.livingCloud.entity.core.Dept;
import cn.livingCloud.entity.core.Employee;
import cn.livingCloud.entity.core.ReportRule;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.entity.user.User;
import cn.livingCloud.entity.user.UserAccount;
import cn.livingCloud.repository.EmployeeMapper;
import cn.livingCloud.repository.MemberMapper;
import cn.livingCloud.repository.ReportRuleMapper;
import cn.livingCloud.utils.DateUtils;
import cn.livingCloud.utils.SessionUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import javax.servlet.http.HttpServletRequest;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by DingJie on 2016/6/30.
 */
public class PreviewDateLoader {
    protected transient ReportRuleMapper mapper = BeanProvider.getBean(ReportRuleMapper.class);
    protected transient MemberMapper memberMapper = BeanProvider.getBean(MemberMapper.class);
    protected transient EmployeeMapper employeeMapper = BeanProvider.getBean(EmployeeMapper.class);
    HttpServletRequest request;
    private String uuid;

    public PreviewDateLoader(String uuid, HttpServletRequest request) {
        this.request = request;
        this.uuid = uuid;
    }

    public String loader() {
        //获取sql和内容
        ReportRule reportRule = mapper.selectByPrimaryKey(uuid);
        String sql = reportRule.getSql();
        String content = reportRule.getContent();
        Dept reportRuleDept = reportRule.getDept();
        //Velocity模板
        VelocityEngine engine = VelocityFactory.getEngine();
        StringWriter sqlWriter = new StringWriter();
        //上下文
        VelocityContext context = new VelocityContext();
        //查询当前用户将条件插入上下文
        Member member = testMember();
        User user = (User) SessionUtils.getCurrentUser();
        UserAccount userAccount = (UserAccount) SessionUtils.getCurrentUserAccount();
        Employee employee = employeeMapper.findEmployeeByMember(member.getUuid());
        Dept dept = employee.getDept();
        List<String> deptUuids = new ArrayList<>();
        ArrayList<String> lists = employee.getDept().getAllSubDeptUuid();
        deptUuids.addAll(lists);
        StringBuilder builder = new StringBuilder();
        for (String deptUuid : deptUuids) {
            builder.append(deptUuid).append(",");
        }
        if (builder.length() > 0) {
            builder.deleteCharAt(builder.length() - 1);
        }
        Map parameterMap = request.getParameterMap();
        context.put("member", member);
        context.put("user", user);
        context.put("currentDateTime", DateUtils.now());
        context.put("userAccount", userAccount);
        context.put("employee", employee);
        context.put("dept", dept);
        context.put("deptUuids", builder);
        context.put("reportRuleDept", reportRuleDept);
        context.put("parameterMap", parameterMap);

        //拼接查询语句
        engine.evaluate(context, sqlWriter, "", sql);
        String executeSql = sqlWriter.toString();

        //获取查询到的数据
        List<Object> result = mapper.executeSql(executeSql);

        StringWriter contentWriter = new StringWriter();
        //将content传入上下文
        context.put("result", result);
        engine.evaluate(context, contentWriter, "", content);
        return contentWriter.toString();
    }

    @Deprecated
    private Member testMember() {
        //如果是测试模式就拿member表中的第一条数据
        if (Config.ENVIRONMENT.equals("development")) {
            MemberListDto listDto = new MemberListDto();
            List<Member> list = memberMapper.getList(listDto);
            return list.get(0);
        }
        return (Member) SessionUtils.getCurrentMember();
    }
}
